package svc

import (
	"database/sql"

	"mall/common/modelx"
	"mall/service/product/repository"
	"mall/service/product/rpc/internal/config"

	"github.com/zeromicro/go-queue/kq"
	"github.com/zeromicro/go-zero/core/logx"
	"github.com/zeromicro/go-zero/core/stores/redis"
)

type ServiceContext struct {
	Config config.Config

	BizRedis                 *redis.Redis
	KqDistributePointsClient *kq.Pusher

	DB                 *sql.DB
	CurrentProduct     repository.CurrentProductRepository
	CurrentProductTerm repository.CurrentProductTermRepository
	Attribute          repository.AttributeRepository

	Product     repository.ProductRepository
	ProductTerm repository.ProductTermRepository
	// ProductUser repository.ProductUserRepository
}

func NewServiceContext(c config.Config) *ServiceContext {
	db, err := modelx.NewMysqlConn(c.Mysql.DataSource)
	if err != nil {
		logx.Must(err)
	}

	rds := redis.MustNewRedis(c.BizRedis)

	return &ServiceContext{
		Config: c,

		BizRedis:                 rds,
		KqDistributePointsClient: kq.NewPusher(c.KqDistributePointsConf.Brokers, c.KqDistributePointsConf.Topic),

		DB:                 db,
		CurrentProduct:     repository.NewCurrentProductRepository(db, c.CacheRedis),
		CurrentProductTerm: repository.NewCurrentProductTermRepository(db, c.CacheRedis),
		Attribute:          repository.NewAttributeRepository(db, c.CacheRedis),

		Product:     repository.NewProductRepository(c.Mysql.DataSource, c.CacheRedis),
		ProductTerm: repository.NewProductTermRepository(c.Mysql.DataSource, c.CacheRedis),
		// ProductUser:  repository.NewProductUserRepository(c.Mysql.DataSource, c.CacheRedis),
	}
}
